package com.yzh.wanandroid.model

import com.yzh.wanandroid.R
import com.yzh.wanandroid.component.app.MainApplication
import com.yzh.wanandroid.utils.*

class LoginUserData {

    private val defaultName = MainApplication.appContext.getString(R.string.go_login)
    private val defaultValue = MainApplication.appContext.getString(R.string.default_value)

    var username = SpUtils.getString(USERNAME, defaultName).toString()
    var level= SpUtils.getString(LEVEL, defaultValue).toString()
    var rank = SpUtils.getString(RANK, defaultValue).toString()

    var errorCode = 0
    var errorMessage = ""

    /**
     * 设置并保存等级
     */
    fun setSaveLevel(level: Int) {
        if (level.toString() != this.level) {
            this.level = level.toString()
            SpUtils.putString(LEVEL, level.toString())
        }
    }

    /**
     * 设置并保存排名
     */
    fun setSaveRank(rank: String) {
        if (rank != this.rank) {
            this.rank = rank
            SpUtils.putString(RANK, rank)
        }
    }

    /**
     * 设置并保存用户名
     */
    fun setSaveUsername(userName: String) {
        if (userName != this.username) {
            this.username = userName
            SpUtils.putString(USERNAME, this.username)
        }
    }

    fun clear() {
        var isNightMode = SpUtils.getBoolean(NIGHT_MODE,false)
        SpUtils.clearAll()
        SpUtils.putBoolean(NIGHT_MODE,isNightMode)
        username = defaultName
        level = defaultValue
        rank = defaultValue
    }
}